<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2022-09-26 13:11:57
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Repositories;

use App\Models\TemporaryToken;
use Illuminate\Support\Str;

class TemporaryTokenRepository extends Repositories
    {

    public $relation_models = ['creator', 'updator', 'deletor', 'user'];

    public function __construct(TemporaryToken $model)
        {
        $this->model = $model;
        }

    public function createToken($user_id = 0, $os = "", $ip = "", $expiresin = -1, $is_system = 0)
        {
        $user_id   = $user_id == 0 ? auth()->user()->employee_id : $user_id;
        $api_token = Str::random(60);
        // $this->whereUserId($user_id)->del();
        $this->create([
            'user_id'   => $user_id,
            'api_token' => $api_token,
            'os'        => $os,
            'ip'        => $ip,
            "expiresin" => $expiresin < 0 ? env("expiresin", 7200) : $expiresin,
            'is_system' => $is_system
        ])->save();
        $count = $this->whereUserId($user_id)->count();
        return ['token' => $api_token, 'count' => 1];
        }
    }
